{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "acceptable-instruction",
   "metadata": {},
   "source": [
    "# Matplotlib入门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "proud-timing",
   "metadata": {},
   "source": [
    "## 导入pyplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "independent-toner",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blank-sensitivity",
   "metadata": {},
   "source": [
    "## 绘制直线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "romance-patrick",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1, 2, 3, 4])\n",
    "plt.plot(x)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "becoming-mystery",
   "metadata": {},
   "source": [
    "## 绘制折线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "marked-universal",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, x ** 2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "painful-europe",
   "metadata": {},
   "source": [
    "## 绘制红点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "subject-lecture",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x, x ** 2, 'rp')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "military-abortion",
   "metadata": {},
   "source": [
    "## 多种绘制样式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hollow-morrison",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.arange(0., 5., 0.2)\n",
    "plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "systematic-fabric",
   "metadata": {},
   "source": [
    "## 使用data参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hazardous-prescription",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'a': np.arange(50),\n",
    "    'c': np.random.randint(0, 50, 50),\n",
    "    'd': np.random.randn(50)\n",
    "}\n",
    "data['b'] = data['a'] + 10 * np.random.randn(50)\n",
    "data['d'] = np.abs(data['d']) * 100\n",
    "\n",
    "plt.scatter('a', 'b', c = 'c', s = 'd', data = data)\n",
    "plt.xlabel('entry a')\n",
    "plt.ylabel('entry b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "separated-interstate",
   "metadata": {},
   "source": [
    "## 绘制类别数据\n",
    "类别数据可直接传入绘制函数。\n",
    "if numrows*numcols<10. So subplot(211) is identical to subplot(2, 1, 1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lesbian-century",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['group_a', 'group_b', 'group_c']\n",
    "values = [1, 10, 100]\n",
    "\n",
    "plt.figure(figsize = (9, 3))\n",
    "plt.subplot(131)\n",
    "plt.bar(names, values)\n",
    "plt.subplot(132)\n",
    "plt.scatter(names, values)\n",
    "plt.subplot(133)\n",
    "plt.plot(names, values)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pharmaceutical-remainder",
   "metadata": {},
   "source": [
    "## 绘制文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recreational-oasis",
   "metadata": {},
   "outputs": [],
   "source": [
    "mu, sigma = 100, 15\n",
    "x = mu + sigma * np.random.randn(10000)\n",
    "\n",
    "# the histogram of the data\n",
    "n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75)\n",
    "\n",
    "plt.xlabel('Smarts')\n",
    "plt.ylabel('Probability')\n",
    "plt.title('Histogram of IQ')\n",
    "plt.text(60, .025, r'$\\mu=100,\\ \\sigma=15$')\n",
    "plt.axis([40, 160, 0, 0.03])\n",
    "plt.grid(True)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welcome-illinois",
   "metadata": {},
   "source": [
    "## 绘制注解文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "alive-banks",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = plt.subplot(111)\n",
    "\n",
    "t = np.arange(0.0, 5.0, 0.01)\n",
    "s = np.cos(2*np.pi*t)\n",
    "line, = plt.plot(t, s, lw=2)\n",
    "\n",
    "plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),\n",
    "             arrowprops=dict(facecolor='black', shrink=0.05),\n",
    "             )\n",
    "\n",
    "plt.ylim(-2, 2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "noted-affiliation",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
